
layui.use(['layer', 'form', 'admin', 'laydate', 'ax','upload','layedit'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var upload = layui.upload;
    var layedit = layui.layedit;
    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取用户信息
    var ajax = new $ax(Feng.ctxPath + "/drug/getDrugInfo?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    console.log(result.data);
    form.val('drugForm', result.data);
    $('#preShow').attr("src","/assets/img/"+result.data.imgUrl);

    form.on('select(brandId)',function (data){
        var e = data.elem;
        //获取选中的值
        console.log(data.value);
        //获取选中的文本
        var text =e[e.selectedIndex].text;
        $("#brandName").val(text);
        console.log(text);
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var val = $("#id").val();
        var text = $("#id").text();
        console.log(val + ";" + text);
        var ajax = new $ax(Feng.ctxPath + "/drug/edit", function (data) {
            Feng.success("修改成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });

    var uploadInst = upload.render({
        elem: '#socialImage',
        url: Feng.ctxPath + '/mgr/upload', // 上传接口
        number: 1,
        exts: 'jpg|jpeg|png|',
        before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            console.log(obj);
            obj.preview(function(index, file, result){
                console.log(index);
                $('#preShow').attr('src', result); //图片链接（base64）
            });
        },
        done: function(res){
            console.log("上传结束：" + res.path);
            $('#img_url').val(res.path);
        },
        error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });
});